package com.phc.a20200511work.paopao;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.phc.a20200511work.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 版权：没有版权 看得上就用
 *
 * @author peng
 * 创建日期：2020/5/12 22
 * 描述：
 */
public class paopaoAdapter extends RecyclerView.Adapter<paopaoAdapter.ViewHolder> {
    List<InfoBean> infoBeans = new ArrayList<>();
    Context context;

    public paopaoAdapter(List<InfoBean> infoBeans, Context context) {
        this.infoBeans = infoBeans;
        this.context = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.paopao_goods_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Picasso.get().load(infoBeans.get(position).getPic()).into(holder.paopaoGoodsItemImage);
        holder.paopaoGoodsItemName.setText(infoBeans.get(position).getTitle());
        holder.paopaoGoodsItemFenlei.setText(infoBeans.get(position).getKind());
        holder.paopaoGoodsItemEdit.setText(String.valueOf(infoBeans.get(position).getCount()));
        //计算
        holder.paopaoGoodsItemHowmach.setText(
                String.valueOf(infoBeans.get(position).getCount()*infoBeans.get(position).getPrice()));

        //购买按钮的点击事件
        final SharedPreferences buyCar = context.getSharedPreferences("buyCar", Context.MODE_PRIVATE);
        holder.paopaoGoodsItemBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将当前商品信息存储进sp
                buyCar.edit().putString(infoBeans.get(position).getTitle(),infoBeans.get(position).getTitle()).apply();
                buyCar.edit().putString(infoBeans.get(position).getTitle(),infoBeans.get(position).getTitle()).apply();
            }
        });
    }

    @Override
    public int getItemCount() {
        return infoBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView paopaoGoodsItemImage;
        TextView paopaoGoodsItemName;
        TextView paopaoGoodsItemFenlei;
        Button paopaoGoodsItemMin;
        EditText paopaoGoodsItemEdit;
        Button paopaoGoodsItemAdd;
        TextView paopaoGoodsItemHowmach;
        Button paopaoGoodsItemBuy;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            paopaoGoodsItemImage = (ImageView) itemView.findViewById(R.id.paopao_goods_item_image);
            paopaoGoodsItemName = (TextView) itemView.findViewById(R.id.paopao_goods_item_name);
            paopaoGoodsItemFenlei = (TextView) itemView.findViewById(R.id.paopao_goods_item_fenlei);
            paopaoGoodsItemMin = (Button) itemView.findViewById(R.id.paopao_goods_item_min);
            paopaoGoodsItemEdit = (EditText) itemView.findViewById(R.id.paopao_goods_item_edit);
            paopaoGoodsItemAdd = (Button) itemView.findViewById(R.id.paopao_goods_item_add);
            paopaoGoodsItemHowmach = (TextView) itemView.findViewById(R.id.paopao_goods_item_howmach);
            paopaoGoodsItemBuy = (Button) itemView.findViewById(R.id.paopao_goods_item_buy);
        }
    }
}
